{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Reading mat files\n",
      "=================",
      "\n",
      "Here are examples of how to read two variables `lat` and `lon` from a mat file called \"test.mat\".\n",
      "\n",
      "= Matlab up to 7.1 =\n",
      "mat files created with Matlab up to version 7.1 can be read using the `mio` module part of `scipy.io`. Reading structures (and arrays of structures) is supported, elements are accessed with the same syntax as in Matlab: after reading a structure called e.g. `struct`, its `lat` element can be obtained with `struct.lat`, or `struct.__getattribute__('lat')` if the element name comes from a string.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "#!/usr/bin/env python\n",
      "from scipy.io import loadmat\n",
      "x = loadmat('test.mat')\n",
      "lon = x['lon']\n",
      "lat = x['lat']\n",
      "# one-liner to read a single variable\n",
      "lon = loadmat('test.mat')['lon']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matlab 7.3 and greater\n",
      "======================\n",
      "\n",
      "Beginning at release 7.3 of Matlab, mat files are actually saved using\n",
      "the HDF5 format by default (except if you use the -vX flag at save time,\n",
      "see in Matlab). These files can be read in Python using, for instance,\n",
      "the [PyTables](http://www.pytables.org/moin) or\n",
      "[h5py](http://h5py.alfven.org/) package. Reading Matlab structures in\n",
      "mat files does not seem supported at this point."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "#!/usr/bin/env python\n",
      "import tables\n",
      "file = tables.openFile('test.mat')\n",
      "lon = file.root.lon[:]\n",
      "lat = file.root.lat[:]\n",
      "# Alternate syntax if the variable name is in a string\n",
      "varname = 'lon'\n",
      "lon = file.getNode('/' + varname)[:]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
